// Dwarf.cs using System.Data; using System.Data.SqlClient; using System.Runtime.CompilerServices; using System.EnterpriseServices; using System.Reflection; using Database; //Registration details. //COM+ application name as it appears in the COM+ catalog. [assembly: ApplicationName("Dwarf")] [assembly: ApplicationActivation(ActivationOption.Library)] //Strong name for assembly. [assembly: AssemblyKeyFileAttribute("components/Dwarf.snk")] [assembly: AssemblyVersion("1.0.0.0")] namespace Dwarf { [Transaction(TransactionOption.Required)] public class Dwarf : ServicedComponent { // is "holdlock" necessary/useful ? private static string select = "select d.dwarf_id as dwarf_id," + "d.dwarf_name as dwarf_name," + "d.born as born," + "d.home_id as home_id," + "mountain_name as home_name," + "d.spouse_id," + "s.dwarf_name as spouse_name " + "from dwarf d holdlock, dwarf s, mountain m " + "where d.spouse_id *= s.dwarf_id and d.home_id *= m.mountain_id"; [AutoComplete] public DataSet GetAll(){ Database.Database db = new Database.Database(); return db.RunSql(select); } [AutoComplete] public DataSet GetSingle(){ Database.Database db = new Database.Database(); return db.RunSql(select + " and d.spouse_id is null"); } [AutoComplete] public DataSet Get(int id){ Database.Database db = new Database.Database(); SqlParameter[] prams = { Database.Database.MakeParam("@id", SqlDbType.Int, id) }; string lselect = select + " and d.dwarf_id = @id"; return db.RunSql(lselect,prams); } [AutoComplete] // returns dwarf_id public int Add( string dwarf_name, int born, int home_id) { Database.Database db = new Database.Database(); SqlParameter[] prams = { Database.Database.MakeParam("@dwarf_name", SqlDbType.VarChar, dwarf_name), Database.Database.MakeParam("@born", SqlDbType.Int, born), Database.Database.MakeParam("@home_id", SqlDbType.Int, home_id,true), }; db.RunSql( "insert into dwarf (dwarf_name,born,home_id) values (@dwarf_name,@born,@home_id)", prams); SqlParameter[] prams2 = { Database.Database.MakeParam("@dwarf_name", SqlDbType.VarChar, dwarf_name), }; DataSet ds = db.RunSql("select dwarf_id from dwarf where dwarf_name = @dwarf_name", prams2); return System.Int32.Parse(ds.Tables[0].Rows[0]["dwarf_id"].ToString()); } [AutoComplete] // if spouse_id is modified, should be called in larger DwarfMgr.Marry() // transaction which updates spouse public void Update(int dwarf_id, string dwarf_name, int born, int home_id, int spouse_id) { Database.Database db = new Database.Database(); SqlParameter[] prams = { Database.Database.MakeParam("@dwarf_id", SqlDbType.Int, dwarf_id), Database.Database.MakeParam("@dwarf_name", SqlDbType.VarChar, dwarf_name), Database.Database.MakeParam("@born", SqlDbType.Int, born), Database.Database.MakeParam("@home_id", SqlDbType.Int, home_id,true), Database.Database.MakeParam("@spouse_id", SqlDbType.Int, spouse_id,true), }; db.RunSql( "update dwarf set dwarf_name = @dwarf_name,"+ "born = @born," + "home_id = @home_id," + "spouse_id = @spouse_id " + "where dwarf_id = @dwarf_id", prams); } [AutoComplete] public void Update(int dwarf_id, string dwarf_name, int born, int home_id) { Dwarf dw = new Dwarf(); DataSet ds = dw.Get(dwarf_id); int spouse_id; if(ds.Tables[0].Rows[0]["spouse_id"] == System.DBNull.Value){ spouse_id = 0; }else{ spouse_id = System.Int32.Parse(ds.Tables[0].Rows[0]["spouse_id"].ToString()); } dw.Update(dwarf_id,dwarf_name,born,home_id,spouse_id); } } }